﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using V2TEK.NET.SH.Silverlight.Charts.GanttHelper;

namespace V2TEK.NET.SH.Silverlight.Charts.TreeGridHelper
{
    public class DynamicTaskExpander : SimpleTaskExpander
    {
        private bool hideBtn = false;

        protected override void InvalidateIcon()
        {
            if (this.ExpanderBtn != null)
            {
                if (!DataItem.EnableExpand)
                {
                    this.ExpanderBtn.Visibility = Visibility.Collapsed;
                }
                else
                {
                    this.ExpanderBtn.Content = this.BtnText;
                    this.ExpanderBtn.Visibility = Visibility.Visible;
                }
            }
        }

        protected override void ExpanderBtnClick()
        {
            if (this.IsLoading)
                return;
            RaiseExpandChange(!this.IsExpand);
            this.IsExpand = !this.IsExpand;
            //this.Update();
            this.InvalidateIcon();
        }

        public void HideExpander()
        {
            DataItem.EnableExpand = false;
            if (this.DataItem.Data.Children.Count > 0)
                return;
            hideBtn = true;
            this.ExpanderBtn.Visibility = System.Windows.Visibility.Collapsed;
        }
        public void ShowExpander()
        {
            DataItem.EnableExpand = true;
            hideBtn = false;
            this.ExpanderBtn.Visibility = System.Windows.Visibility.Visible;
        }

        public void Expand()
        {
            this.IsExpand = true;
        }

        public void Collapse()
        {
            this.IsExpand = false;
        }

        public void BeginLoading()
        {
            _isLoading = true;
            //DataItem.Tag = "loading";
            this.ExpanderBtn.IsEnabled = false;
        }
        public void EndLoading()
        {
            _isLoading = false;
            //DataItem.Tag = null;
            this.ExpanderBtn.IsEnabled = true;
        }

        private static bool _isLoading = false;
        public bool IsLoading
        {
            get
            {
                return _isLoading;
                //return string.Equals(DataItem.Tag, "loading");
            }
        }
    }
}
